package com.ruoyi.project.cms.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.cms.domain.Category;
import com.ruoyi.project.cms.service.CategoryService;

@Controller
@RequestMapping("/site/category")
public class CategoryController extends BaseController {
	private String prefix = "cms/category";

	@Autowired
	private CategoryService categoryService;

	@RequiresPermissions("cms:category:view")
	@GetMapping()
	public String category() {
		return prefix + "/category";
	}

	/**
	 * 查询栏目列表
	 */
	@RequiresPermissions("cms:category:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Category Category) {
		startPage();
		List<Category> list = categoryService.selectCategoryList(Category);
		return getDataTable(list);
	}

	/**
	 * 导出栏目列表
	 */
	@RequiresPermissions("cms:category:export")
	@PostMapping("/export")
	@ResponseBody
	public AjaxResult export(Category Category) {
		List<Category> list = categoryService.selectCategoryList(Category);
		ExcelUtil<Category> util = new ExcelUtil<Category>(Category.class);
		return util.exportExcel(list, "category");
	}

	/**
	 * 新增栏目
	 */
	@GetMapping("/add")
	public String add() {
		return prefix + "/add";
	}

	/**
	 * 新增保存栏目
	 */
	@RequiresPermissions("cms:category:add")
	@Log(title = "栏目", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Category Category) {
		return toAjax(categoryService.insertCategory(Category));
	}

	/**
	 * 修改栏目
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Long id, ModelMap mmap) {
		Category Category = categoryService.selectCategoryById(id);
		mmap.put("Category", Category);
		return prefix + "/edit";
	}

	/**
	 * 修改保存栏目
	 */
	@RequiresPermissions("cms:category:edit")
	@Log(title = "栏目", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Category Category) {
		return toAjax(categoryService.updateCategory(Category));
	}

	/**
	 * 删除栏目
	 */
	@RequiresPermissions("cms:category:remove")
	@Log(title = "栏目", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		return toAjax(categoryService.deleteCategoryByIds(ids));
	}
}
